package com.kingyea.app.market.applicationType.controller;

import com.kingyea.app.market.appBaseInfo.bean.AppBaseInfo;
import com.kingyea.app.market.applicationType.bean.ApplicationType;
import com.kingyea.app.market.applicationType.service.ApplicationTypeService;
import com.kingyea.app.market.client.AppBaseInfoServiceClient;
import com.kingyea.app.market.client.UserServiceClient;
import com.kingyea.app.market.market.bean.Market;
import com.kingyea.app.market.utils.page.PageInfo;
import com.kingyea.mobilepolice.system.Result;
import com.kingyea.mobilepolice.user.bean.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @version v1.0.0
 * @description ApplicationType 控制层
 * @date Created in 2018-02-10 16:14:42
 */
@RequestMapping(value = "/applicationType")
@RestController
public class ApplicationTypeController {

    private static final Logger LOGGER = LoggerFactory
            .getLogger(ApplicationTypeController.class);

    @Autowired
    private ApplicationTypeService applicationTypeService;
    @Autowired
    private AppBaseInfoServiceClient AppBaseInfoServiceClient;
    @Autowired
    private UserServiceClient userServiceClient;

    /**
     * 查询全部
     *
     * @param
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Object list() throws Exception {
        return applicationTypeService.list(null);
    }

    /**
     * 根据条件查询
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/condition", method = RequestMethod.POST)
    public Object conditionQuery(
            @RequestBody Map<String, Object> applicationType) throws Exception {
        return applicationTypeService.list(applicationType);
    }

    /**
     * 通过ID查找对象
     *
     * @param classCode
     * @return
     */
    @RequestMapping(value = "/{classCode}", method = RequestMethod.GET)
    public ApplicationType detail(@PathVariable Integer classCode)
            throws Exception {
        return applicationTypeService.getById(classCode);
    }

    /**
     * 创建
     *
     * @param obj
     * @return
     */
    @RequestMapping(value = "/addApplicationType", method = RequestMethod.POST)
    public Object create(@RequestBody Map<String, Object> obj)
            throws Exception {
        //判断是否已经存在
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("marketCode", obj.get("marketCode"));
        map.put("superClass", obj.get("superClass"));
        map.put("_className", obj.get("className"));
        PageInfo re = (PageInfo) applicationTypeService.list(map);
        List<Market> list = re.getList();
        if (list.size() > 0) {
            Result results = new Result();
            results.setCode("B100001");
            return results;
        }
        obj.put("logo", obj.get("_logo"));
        Result<User> result = userServiceClient.getCurrentUserInfo();
        User user = result.getData();
        obj.put("creater", user.getName());
        return applicationTypeService.save(obj);
    }

    /**
     * 更新
     *
     * @param applicationType
     * @return
     */
    @RequestMapping(value = "/editApplicationType", method = RequestMethod.POST)
    public Object update(@RequestBody ApplicationType applicationType)
            throws Exception {
        return applicationTypeService.update(applicationType);
    }

    /**
     * 删除
     *
     * @param applicationType
     * @return
     */
    @RequestMapping(value = "/deleteApplicationType", method = RequestMethod.POST)
    public Object delete(@RequestBody ApplicationType applicationType) {
        int deleteById = 0;
        try {
            Result resultObj = AppBaseInfoServiceClient
                    .getByAppType(applicationType.getClassCode());
            List<AppBaseInfo> byAppType = (List<AppBaseInfo>) resultObj
                    .getData();
            if (byAppType.size() == 0) {
                deleteById = applicationTypeService.deleteById(applicationType
                        .getClassCode());
            } else {
                Result results = new Result();
                results.setCode("B100001");
                return results;
            }
        } catch (Exception e) {
        }
        return deleteById;
    }

    /**
     * 通过 应用市场查找
     *
     * @param marketCode
     * @return
     */
    @RequestMapping(value = "/market/{marketCode}", method = RequestMethod.GET)
    public List<ApplicationType> getByMarket(
            @PathVariable(value = "marketCode") String marketCode) {
        List<ApplicationType> byMarket = null;
        try {
            byMarket = applicationTypeService.getByMarket(marketCode);
        } catch (Exception e) {
            e.printStackTrace();
            // TODO: handle exception
        }
        return byMarket;

    }
}
